package com.example.demo.service.Impl;

import com.example.demo.mapper.LifebookMapper;
import com.example.demo.pojo.Lifebook;
import com.example.demo.pojo.Module;
import com.example.demo.service.LifebookService;
import com.example.demo.utils.AliOSSUtils;
import com.example.demo.utils.WatermarkUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.awt.*;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class LifebookServiceImpl implements LifebookService {
    @Autowired
    private LifebookMapper lifebookMapper;
    @Autowired
    private WatermarkUtils watermarkUtils;
    @Override
    public PageInfo queryLifeBook(Integer page, Integer limit, String uuid) {
        //    1.获取总记录数
        PageHelper.startPage(page,limit);
        //    2.执行操作
        List<Lifebook> list = lifebookMapper.queryLifeBook(uuid);
        System.out.println(page+"===="+limit+"====="+list.size());
        PageInfo<Lifebook> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    @Override
    public Integer queryLifeBook01() {
        return lifebookMapper.queryLifeBook01();
    }

    @Override
    public PageInfo queryLifeBook02(Integer page, Integer limit, Lifebook lifebook) {
        //    1.获取总记录数
        PageHelper.startPage(page,limit);
        List<Lifebook> list = lifebookMapper.queryLifeBook02(lifebook);
        PageInfo<Lifebook> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void addLifeBook(Lifebook lifebook) {
        String uuid = UUID.randomUUID().toString().replace("-","");
        lifebook.setUuid(uuid);
        lifebookMapper.addLifeBook(lifebook);
    }

    @Override
    public void deleteLifeBook(String id) {
        lifebookMapper.deleteLifeBook(id);
    }
        @Override
    public void updateLifeBook(Lifebook lifebook) {
        lifebookMapper.updateLifeBook(lifebook);
    }

    @Override
    public void updateLifeBookphoto(String photo, String id) {
        lifebookMapper.updateLifeBookphoto(photo,id);
    }
    //图片导出返回图片url

    @Override
    public byte[] lifebookExport(String inner) {
        //水印字体
        Font font = new Font("隶书", Font.BOLD, 70);
        String lifebookImg = "src/main/resources/lifebook_img/生死簿01.png";
        /**给图片添加文字水印**/
        String image = "src/main/resources/lifebook_img/生死簿02.png";
        watermarkUtils.addTextWatermark (lifebookImg, image,inner, Color.DARK_GRAY,font, 0.85F);
        //ClassPathResource resource = new ClassPathResource(image);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream("src/main/resources/lifebook_img/生死簿02.png");
            byte[] bytes = inputStream.readAllBytes();
            inputStream.close();
            return bytes;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    @Override
    public int queryLifeBookAfterlife01() {
        return lifebookMapper.queryLifeBookAfterlife01();
    }

    @Override
    public int queryLifeBookAfterlife02() {
        return lifebookMapper.queryLifeBookAfterlife02();
    }
    // 更新数据
    @Override
    public void xiugai() {
        // 将死后的人改为待勾魂状态
        lifebookMapper.deathstate01();
        // 将该出生的人出生
        lifebookMapper.chusheng();
        

    }

    @Override
    public void addmerit(String lifebook) {
        lifebookMapper.addmerit(lifebook);
    }

    @Override
    public int delchange(int i) {
        return lifebookMapper.delchange(i);
    }

    @Override
    public int lunhunchange(int i) {
        return lifebookMapper.lunhunchange(i);
    }

    @Override
    public int diyuchange(int i) {
        return lifebookMapper.diyuchange(i);
    }

    @Override
    public List<Module> getuserlevel() {
        return lifebookMapper.getuserlevel();
    }
    //图片上传
}
